import model.*;

public final class MyStrategy implements Strategy {


    @Override
    public void move(Hockeyist self, World world, Game game, Move move) {
        initialise(self,world,game,move);

        if (self.getState() == HockeyistState.SWINGING) {
            move.setAction(ActionType.STRIKE);
            return;
        }
        move.setAction(ActionType.TAKE_PUCK);

        if (world.getPuck().getOwnerPlayerId() == self.getPlayerId()) {         //шайба у моей комманды
            if (world.getPuck().getOwnerHockeyistId() == self.getId()) {                //чел с шайбой
                if(HockeyistsMethods.onStrikePos(self)){                                    //на позиции для удара
                    HockeyistsMethods.breakHock(self,move);
                    HockeyistsMethods.aimAndSwing(self,game,move);                                //поворот и замах
                    return;
                }else{                                                                          //не на позиции для удара
                    if(!HockeyistsMethods.enemyesBehind(self,world,game,move)) {
                        Hockeyist friendAttacker = (HockeyistsMethods.getAttackerFriend(self, world, game, move));
                        if (HockeyistsMethods.onStrikePos(friendAttacker) &&
                                HockeyistsMethods.targetLooksAtMe(self, friendAttacker)) {         //тиммейт на позиции для удара и смотрит на меня
                            HockeyistsMethods.rotateAndPassToTarget(self, friendAttacker, move);      //повернуться и пасс
                            return;
                        } else {                                                                              //тиммейт не готов
                            if (HockeyistsMethods.enemyesBehind(self, world, game, move)) {
                                HockeyistsMethods.goToStrikePos(self, world, move);
                                return;
                            } else {
                                HockeyistsMethods.moveForSavinPuck(self, world, game, move);                               //пытаться сохранить шайбу
                                return;
                            }
                        }
                    }else{
                        HockeyistsMethods.goToStrikePos(self, world, move);
                        return;
                    }
                }
            } else {//чел без шайбы
                if(HockeyistsMethods.onStrikePos(self)){
                    HockeyistsMethods.breakHock(self,move);
                    HockeyistsMethods.lookAtPuck(self,world,move);
                    return;
                }else{
                    HockeyistsMethods.goToStrikePos(self, world, move);
                    return;
                }
            }
        } else {//шайба у противника
            move.setSpeedUp(1.0D);
            move.setTurn(self.getAngleTo(world.getPuck()));
            move.setAction(ActionType.TAKE_PUCK);
        }
    }
    private void initialise(Hockeyist self, World world, Game game, Move move) {
        if (Sync.firstTick) {
            Player opponentPlayer = world.getOpponentPlayer();
            Sync.gatesCenterX = 0.5D * (opponentPlayer.getNetBack() + opponentPlayer.getNetFront());
            Sync.gatesCenterY = 0.5D * (opponentPlayer.getNetBottom() + opponentPlayer.getNetTop());

            if (Sync.gatesCenterX > world.getPuck().getX())
                Sync.gatesRightSide = true;
            else
                Sync.gatesRightSide = false;


            if(!Sync.gatesRightSide) {
                Sync.strikePos[0] = game.getRinkLeft() + world.getWidth() / 6;
                Sync.strikePos[1] = game.getRinkTop() + world.getHeight() / 6;
                Sync.strikePos[2] = game.getRinkLeft() + world.getWidth() / 3;
                Sync.strikePos[3] = game.getRinkBottom() - world.getHeight() / 6;
            }else{
                Sync.strikePos[0] = game.getRinkRight() - world.getWidth() / 3;
                Sync.strikePos[1] = game.getRinkTop() + world.getHeight() / 6;
                Sync.strikePos[2] = game.getRinkRight() - world.getWidth() / 6;
                Sync.strikePos[3] = game.getRinkBottom() - world.getHeight() / 6;
            }

            Sync.firstTick = false;
        }
    }
}